/* 
 * File:   main.cpp
 * Author: frank
 *
 * Created on 2013年10月23日, 下午8:57
 */

#include<iostream>
#include<string>
#include"FileMng.h"
using namespace std;

BaseSect baseMemo;
MemoOpenFile openFileMemo[OPEN_MAX];
int openedSize;
int openedSum;
char strMemo[STRMEMO_MAX][CONTENT_MAX];

int main()
{
	char cmd[10],name[20],newName[20],path[50];
	char opt;
	char filename[50],loadTypeStr[5];
	short loadType;
	int res;
	long modTime;

//	 HDformat();

	iniMemo();

	//用户登录
	login(name);

	res=FAIL;
	while(res==FAIL){
		res=addOnline(name);
	}
        printf("\n*******************************\n");
	printf("    User %s is online NOW",name);
        printf("\n*******************************\n");

	//输入命令，并执行
	while(1)
	{       
                printf("\n\n");
                if(strcmp(name,"root")==0)
                    printf(" [format] Format harddisk   [adduser] Add a user  [deluser] Delete a user\n");
		printf(" [load] Load a file to the memory\t [unload] Unload a file from memory\n");
                printf(" [show] Show the content of a file\t [edit] Edit the content of a file\n");
		printf(" [create] Create a new file\t\t [delete] Delete a file\n");
                printf(" [quit] Quit\n");
		cin>>cmd;
		if(strcmp(cmd,"create")==0){//创建文件
			printf("Please input the directory that the file will be created:");
			cin>>path;
			//是否在自己的路径下
			if(isOwner(name,path)!=1){
				printf("Sorry! NO PERMISSION to create a file in directories of other users!\n");
				continue;
			}
			printf("Please input the file name:");
			cin>>filename;

			createFile(path,filename,name);
		}
		else if(strcmp(cmd,"load")==0){   //打开文件
			printf("Please input the file name that you want to load:");
			cin>>filename;
			printf("Please choose the type of loading(r: READ ONLY w:WRITE ONLY rw:READ AND WRITE):");
			while(1){
				cin>>loadTypeStr;
				if(strcmp(loadTypeStr,"rw")==0){ 
					loadType=RANDW; 
					break;
				}
				else if(strcmp(loadTypeStr,"r")==0){
					loadType=READ;
					break;
				}
				else if(strcmp(loadTypeStr,"w")==0){
					loadType=WRITE;
					break;
				}
				else{
					printf("The value you entered is NOT valid, please input again!\n");
				}
			} 
	
			res=loadFile(filename,loadType,name);
			if(res!=FAIL)
				cout<<"Load SUCCESSFULY!\n";
		}
		else if(strcmp(cmd,"delete")==0) {  //删除文件
			printf("Please input the file name that you want to delete:");
			cin>>filename;
			res=deleteFile(filename,name);
			if(res!=FAIL)
				printf("Delete SUCCESSFULY!\n");
			else
				printf("Delete FAILED!\n");
		}
		else if(strcmp(cmd,"unload")==0){   //关闭文件
			printf("Please input the file name that you want to unload:");
			cin>>filename;
			res=unloadFile(filename,name);
			if(res!=FAIL)
				cout<<"Unload SUCCESSFULY!\n";
		}
		else if(strcmp(cmd,"show")==0){    //显示文件
			printf("Please input the file name that you want to show:(the type of loading is READ ONLY or READ AND WRITE):");
			cin>>filename;
			res=showFile(filename);
		}
		else if(strcmp(cmd,"edit")==0){    //写文件
			printf("Please input the file name that you want to edit:(the type of loading is WRITE ONLY or READ AND WRITE):");
			cin>>filename;
			res=editFile(filename,name);
		}
		else if(strcmp(cmd,"quit")==0){
			//检查是否关闭所有文件
			if(openedSum!=0)
				printf("Please UNLOAD all of file before quit!\n");
			else				
				break;
		}
                else if(strcmp(cmd,"format")==0){
                        if(strcmp(name,"root")){
                            printf("The value you entered is NOT valid!\n");
                            continue;
                        }
                        cout<<"\n**********************************\n";
                        cout<<"    CONFIRM:[Y]Format [N]Cancel";
                        cout<<"\n**********************************\n";
                        while(1){
                                cin>>opt;
                                if(opt=='y' || opt=='Y'){
                                    HDformat();
                                    addOnline(name);
                                    break;
                                }
                                else if(opt=='n'|| opt=='N')
                                    break;
                                else
                                    printf("The value you entered is NOT valid, please input again:");
                        }

                }
                else if(strcmp(cmd,"adduser")==0){
                        if(strcmp(name,"root")){
                            printf("The value you entered is NOT valid!\n");
                            continue;
                        }

			time(&modTime);
			res=addUser(newName,modTime);
			if(res!=FAIL){
				addUserHead(newName,modTime);
				printf("User %s is SUCCESSFULY added!\n",newName);
			}

                }
                else if(strcmp(cmd,"deluser")==0){
                        if(strcmp(name,"root")){
                            printf("The value you entered is NOT valid!\n");
                            continue;
                        }
                        
                        res=onlyRoot();
			if(res==FAIL)
				continue;
			res=delUser(newName);
			if(res!=FAIL){
				delUserHead(newName);
				printf("User %s is SUCCESSFULY deleted!\n",newName);
			}
                }
		else 
			printf("The value you entered is NOT valid!\n");

		showOpen();
	}
	res=FAIL;
	while(res==FAIL){
		res=delOnline(name);
	}

	return 0;
}

